/*
 * DesignAssignment3.c
 *
 * Created: 3/13/2014 12:22:34 PM
 *  Author: tawataob
 */ 


#include <avr/io.h>
#include <avr/interrupt.h>

ISR(TIMER1_OVF_vect)
{
	cli(); // disable interrupts
	
	TIFR1 = 0x02; // Clear TIFR1
	TCCR0A = 0x00; // for normal mode
	TCCR0B = 0x06; // WGM = 000 for normal mode, CS = 110 for external connect to T0
	
	PORTC ^= 0x01; // toggles PC0, the external signal for INT0
	
	if(TCNT0%16 == 0)
	{
		PORTC ^= 0x02; // toggle PC1, the 9th LED, every 16 falling edges
	}

	PORTB = TCNT0; // outputting counter0 to PortB
	TCNT1 = 65292; // timer1 counter value for a period of 0.5seconds and a 50% duty cycle
	TCCR1A = 0x00; // for normal mode
	TCCR1B = 0x05; // WGM = 000 for normal mode, CS = 101, prescaler = 1024
	sei(); // enable interrupts
}

int main(void)
{
	DDRB = 0xFF; // set all of PortB to output
	DDRC = 0x03; // set PC0 and PC1 as output
	PORTB = 0x00; // clear PortB
	PORTC = 0x00; // clear PortC
	TIMSK1 = 0x01; // enable the counter overflow interrupt
	sei(); // enable interrupts
	TCNT1 = 65292; // timer1 counter value for a period of 0.5seconds and a 50% duty cycle
	TCCR1A = 0x00; // for normal mode
	TCCR1B = 0x05; // WGM = 000 for normal mode, CS = 101, prescaler = 1024
	while(1)
	{
		// timer1/counter1 increases here until it overflows and throws and exception
	}
}